package com.jianshuge.app.ui;

import greendroid.widget.MyQuickAction;
import greendroid.widget.QuickActionGrid;
import greendroid.widget.QuickActionWidget;
import greendroid.widget.QuickActionWidget.OnQuickActionClickListener;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.jianshuge.app.ui.Main.TweetReceiver;

import com.jianshuge.app.ui.Main;
import com.jianshuge.app.AppConfig;
import com.jianshuge.app.AppContext;
import com.jianshuge.app.AppException;
import com.jianshuge.app.R;

import com.jianshuge.app.adapter.ListViewRecentTrendsAdapter;
import com.jianshuge.app.adapter.ListViewMyTrendsAdapter;
import com.jianshuge.app.adapter.ListViewBooklistAdapter;
import com.jianshuge.app.adapter.ListViewBookshelfAdapter;

import com.jianshuge.app.bean.Booklist;
import com.jianshuge.app.bean.BooklistList;

import com.jianshuge.app.bean.Behavior;
import com.jianshuge.app.bean.BehaviorList;

import com.jianshuge.app.bean.Bookshelf;
import com.jianshuge.app.bean.BookshelfList;

import com.jianshuge.app.bean.Notice;
import com.jianshuge.app.bean.MyResult;

import com.jianshuge.app.common.StringUtils;
import com.jianshuge.app.common.UIHelper;

import com.jianshuge.app.common.UpdateManager;
import com.jianshuge.app.widget.BadgeView;
import com.jianshuge.app.widget.NewDataToast;
import com.jianshuge.app.widget.PullToRefreshListView;
import com.jianshuge.app.widget.ScrollLayout;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.TextView;

public class Main extends BaseActivity {
	
	public static final int QUICKACTION_SEARCH = 0;
	public static final int QUICKACTION_PUB_BOOKLIST = 1;
	public static final int QUICKACTION_NEW_BOOK = 2;
	public static final int QUICKACTION_USERINFO = 3;
	public static final int QUICKACTION_SETTING = 4;
	public static final int QUICKACTION_EXIT = 5;
	
	private ScrollLayout mScrollLayout;
	private RadioButton[] mButtons;
	private String[] mHeadTitles;
	private int mViewCount;
	private int mCurSel;

	private ImageView mHeadLogo;
	private TextView mHeadTitle;
	private ProgressBar mHeadProgress;
	private ImageButton mHead_search;
	private ImageButton mHeadPub_post;
	private ImageButton mHeadPub_tweet;

	private int curTrendsCatalog = -1;
	private int curBooklistCatalog = 1;
	private int curMyBooklistCatalog = 3;
	private int curMyTrendsCatalog = 74;
	private int curMyBookshelfCatalog = 74;
	private int curMyMessageCatalog = 74;
	
	private PullToRefreshListView lvRecentTrends;
	private PullToRefreshListView lvBooklist;
	private PullToRefreshListView lvMyBooklist;
	private PullToRefreshListView lvMyTrends;
	private PullToRefreshListView lvMyBookshelf;
	private PullToRefreshListView lvMyMessage;
	
	private ListViewRecentTrendsAdapter lvRecentTrendsAdapter;
	private ListViewBooklistAdapter 	lvBooklistAdapter;
	private ListViewBooklistAdapter 	lvMyBooklistAdapter;
	private ListViewMyTrendsAdapter 	lvMyTrendsAdapter;
	private ListViewBookshelfAdapter 	lvMyBookshelfAdapter;
	private ListViewMyTrendsAdapter 	lvMyMessageAdapter;
	
	private List<Behavior> lvRecentTrendsData = new ArrayList<Behavior>();
	private List<Booklist> lvBooklistData = new ArrayList<Booklist>();
	private List<Booklist> lvMyBooklistData = new ArrayList<Booklist>();
	
	private List<Behavior> lvMyTrendsData = new ArrayList<Behavior>();
	private List<Bookshelf> lvMyBookshelfData = new ArrayList<Bookshelf>();
	private List<Behavior> lvMyMessageData = new ArrayList<Behavior>();
	
	private Handler lvRecentTrendsHandler;
	private Handler lvBooklistHandler;
	private Handler lvMyBooklistHandler;
	private Handler lvMyTrendsHandler;
	private Handler lvMyBookshelfHandler;
	private Handler lvMyMessageHandler;
	
	private int lvRecentTrendsSumData;
	private int lvBooklistSumData;
	private int lvMyBooklistSumData;
	private int lvMyTrendsSumData;
	private int lvMyBookshelfSumData;
	private int lvMyMessageSumData;

	private RadioButton fbNews;
	private RadioButton fbQuestion;
	private RadioButton fbTweet;
	private ImageView fbSetting;

	private Button framebtn_trends_lastest;
	private Button framebtn_hot_booklist;
	private Button framebtn_zero_booklist;

	private Button framebtn_followed_booklist;
	private Button framebtn_created_booklist;
	private Button framebtn_part_booklist;
	
	private Button framebtn_my_trends;
	private Button framebtn_my_bookshelf;
	private Button framebtn_my_message;
	
	private View lvTrends_footer;
	private View lvBooklist_footer;
	private View lvMyBooklist_footer;
	private View lvMyTrends_footer;
	private View lvMyBookshelf_footer;
	private View lvMyMessage_footer;

	private TextView lvTrends_foot_more;
	private TextView lvBooklist_foot_more;
	private TextView lvMyBooklist_foot_more;
	private TextView lvMyTrends_foot_more;
	private TextView lvMyBookshelf_foot_more;
	private TextView lvMyMessage_foot_more;
	
	private ProgressBar lvTrends_foot_progress;
	private ProgressBar lvBooklist_foot_progress;
	private ProgressBar lvMyBooklist_foot_progress;
	private ProgressBar lvMyTrends_foot_progress;
	private ProgressBar lvMyBookshelf_foot_progress;
	private ProgressBar lvMyMessage_foot_progress;
		
	public static BadgeView bv_message;
	public static BadgeView bv_tweet;;

	private QuickActionWidget mGrid;// 快捷栏控件

	private boolean isClearNotice = false;
	private int curClearNoticeType = 0;

	private TweetReceiver tweetReceiver;// 动弹发布接收器
	private AppContext appContext;// 全局Context
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		//注册广播接收器	
		tweetReceiver = new TweetReceiver();
		IntentFilter filter = new IntentFilter();
		filter.addAction("com.jianshuge.app.action.APP_TWEETPUB");
		registerReceiver(tweetReceiver, filter);
		
		appContext = (AppContext) getApplication();
		
		//网络连接判断
		if (!appContext.isNetworkConnected()) {
			UIHelper.ToastMessage(this, R.string.network_not_connected);
		}
		
		//初始化登录
		appContext.initLoginInfo();
		
		this.initHeadView();
		this.initFootBar();
		this.initPageScroll();
		this.initFrameButton();
		this.initBadgeView();
		this.initQuickActionGrid();
		this.initFrameListView();
		
		//检查新版本
		if (appContext.isCheckUp()) {
			UpdateManager.getUpdateManager().checkAppUpdate(this, false);
		}

		//启动轮询通知信息
		this.foreachUserNotice();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		if (mViewCount == 0)
			mViewCount = 3;
		if (mCurSel == 0 && !fbNews.isChecked()) {
			fbNews.setChecked(true);
			fbQuestion.setChecked(false);
			fbTweet.setChecked(false);
		}
		// 读取左右滑动配置
		mScrollLayout.setIsScroll(appContext.isScroll());
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		unregisterReceiver(tweetReceiver);
	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);

		if (intent.getBooleanExtra("LOGIN", false)) {
			
			if (mCurSel == 2) {
				if (lvMyMessageData.isEmpty()) {
					this.loadLvMyMessageData(appContext.getLoginUid(), 1, lvMyMessageHandler, UIHelper.LISTVIEW_ACTION_INIT);
				}
			}

		} else if (intent.getBooleanExtra("NOTICE", false)) {
			// 查看最新信息
			mScrollLayout.scrollToScreen(2);
		}
	}

	public class TweetReceiver extends BroadcastReceiver {
		@Override
		public void onReceive(final Context context, Intent intent) {
			int what = intent.getIntExtra("MSG_WHAT", 0);
			if (what == 1) {
				MyResult res = (MyResult) intent.getSerializableExtra("RESULT");
				UIHelper.ToastMessage(context, res.getErrorMessage(), 1000);
				
				if (res.OK()) {
				
				// 	发送通知广播
					if (res.getNotice() != null) {
						UIHelper.sendBroadCast(context, res.getNotice());
					}
					
				// 	发完动弹后-刷新最新动弹、我的动弹&最新动态(当前界面必须是动弹|动态)
				//	loadLvMyMessageData(appContext.getLoginUid(), 1, lvMyMessageHandler, UIHelper.LISTVIEW_ACTION_REFRESH);
					if( mCurSel == 0 ) {
						loadLvTrendsData(curTrendsCatalog, 1, lvRecentTrendsHandler, UIHelper.LISTVIEW_ACTION_REFRESH);
					}		
				}
			} else {
				
				/***
				final Tweet tweet = (Tweet) intent.getSerializableExtra("TWEET");
				***/
				final Handler handler = new Handler() {
					public void handleMessage(Message msg) {
						if (msg.what == 1) {
							MyResult res = (MyResult) msg.obj;
							UIHelper.ToastMessage(context,res.getErrorMessage(), 1000);
							if (res.OK()) {
								// 发送通知广播
								if (res.getNotice() != null) {
									UIHelper.sendBroadCast(context,res.getNotice());
								}
								
								// 发完动弹后-刷新最新、我的动弹&最新动态
								if( mCurSel == 0 )
									loadLvTrendsData(curTrendsCatalog, 1, lvRecentTrendsHandler, UIHelper.LISTVIEW_ACTION_REFRESH);
								/***
								if (TweetPub.mContext != null) {
									// 清除动弹保存的临时编辑内容
									appContext.removeProperty(AppConfig.TEMP_TWEET,AppConfig.TEMP_TWEET_IMAGE);
									((Activity) TweetPub.mContext).finish();
								}
								**/
								
							}
						} else {
							((AppException) msg.obj).makeToast(context);
							/***
							if (TweetPub.mContext != null&&TweetPub.mMessage != null)
								TweetPub.mMessage.setVisibility(View.GONE);
							***/
						}
					}
				};
				
				/***
				Thread thread = new Thread() {
					public void run() {
						Message msg = new Message();
						try {
							Result res = appContext.pubTweet(tweet);
							msg.what = 1;
							msg.obj = res;
						} catch (AppException e) {
							e.printStackTrace();
							msg.what = -1;
							msg.obj = e;
						}
						handler.sendMessage(msg);
					}
				};
				***/
				
				/***
				if (TweetPub.mContext != null)
					UIHelper.showResendTweetDialog(TweetPub.mContext, thread);
				else
					UIHelper.showResendTweetDialog(context, thread);
				***/
			}
		}
	}

	
	/**
	 * 初始化头部视图
	 */
	private void initHeadView() {
		
		mHeadLogo = (ImageView) findViewById(R.id.main_head_logo);
		mHeadTitle = (TextView) findViewById(R.id.main_head_title);
		mHeadProgress = (ProgressBar) findViewById(R.id.main_head_progress);
		mHead_search = (ImageButton) findViewById(R.id.main_head_search);
		mHeadPub_post = (ImageButton) findViewById(R.id.main_head_pub_post);
		mHeadPub_tweet = (ImageButton) findViewById(R.id.main_head_pub_tweet);

		mHead_search.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				UIHelper.showSearch(v.getContext());
			}
		});
		mHeadPub_post.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				UIHelper.showBooklistPub(v.getContext());
			}
		});
		
		mHeadPub_tweet.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {

			}
		});
	}

	/**
	 * 初始化底部栏
	 */
	private void initFootBar() {
		fbNews = (RadioButton) findViewById(R.id.main_footbar_news);
		fbQuestion = (RadioButton) findViewById(R.id.main_footbar_question);
		fbTweet = (RadioButton) findViewById(R.id.main_footbar_tweet);
		fbSetting = (ImageView) findViewById(R.id.main_footbar_setting);		
		fbSetting.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
			//	展示快捷栏&判断是否登录&是否加载文章图片
			//	UIHelper.showSettingLoginOrLogout(Main.this, mGrid.getQuickAction(0));
				mGrid.show(v);
			}
		});
	}
	
	/**
	 * 初始化通知信息标签控件
	 */
	private void initBadgeView() {
		
		bv_tweet = new BadgeView(this, fbTweet);
		bv_tweet.setBackgroundResource(R.drawable.widget_count_bg);
		bv_tweet.setIncludeFontPadding(false);
		bv_tweet.setGravity(Gravity.CENTER);
		bv_tweet.setTextSize(8f);
		bv_tweet.setTextColor(Color.WHITE);

		bv_message = new BadgeView(this, framebtn_my_message);
		bv_message.setBackgroundResource(R.drawable.widget_count_bg);
		bv_message.setIncludeFontPadding(false);
		bv_message.setGravity(Gravity.CENTER);
		bv_message.setTextSize(8f);
		bv_message.setTextColor(Color.WHITE);
	
	}
	
	/**
	 * 初始化水平滚动翻页
	 */
	private void initPageScroll() {
		
		mScrollLayout = (ScrollLayout) findViewById(R.id.main_scrolllayout);
		LinearLayout linearLayout = (LinearLayout) findViewById(R.id.main_linearlayout_footer);
		mHeadTitles = getResources().getStringArray(R.array.head_titles);
		mViewCount = mScrollLayout.getChildCount();
		mButtons = new RadioButton[mViewCount];

		for (int i = 0; i < mViewCount; i++) {
			mButtons[i] = (RadioButton) linearLayout.getChildAt(i * 2);
			mButtons[i].setTag(i);
			mButtons[i].setChecked(false);
			mButtons[i].setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					int pos = (Integer) (v.getTag());
					// 点击当前项刷新
					if (mCurSel == pos) {
						switch (pos) {
						case 0: //广场
							if (lvRecentTrends.getVisibility() == View.VISIBLE)
								lvRecentTrends.clickRefresh();
							else
								lvBooklist.clickRefresh();
							break;
							
						case 1: //我的书单
							lvMyBooklist.clickRefresh();
							break;
						
						case 2://  我的空间=>我的动态 + 我的通知 + 我的私信
							
							if (lvMyTrends.getVisibility() == View.VISIBLE)
								lvMyTrends.clickRefresh();
							else if (lvMyBookshelf.getVisibility() == View.VISIBLE)
								lvMyBookshelf.clickRefresh();
							else
								lvMyMessage.clickRefresh();
							
							break;
							
						}
					}
					mScrollLayout.snapToScreen(pos);
				}
			});
			
		}

		// 设置第一显示屏
		mCurSel = 0;
		mButtons[mCurSel].setChecked(true);

		mScrollLayout.SetOnViewChangeListener(new ScrollLayout.OnViewChangeListener() {
					public void OnViewChange(int viewIndex) {
						// 切换列表视图-如果列表数据为空：加载数据
						switch (viewIndex) {
						
						case 0:
							if (lvRecentTrends.getVisibility() == View.VISIBLE) 
							{	
								if (lvRecentTrendsData.isEmpty()) {
									loadLvTrendsData(curTrendsCatalog, 1, lvRecentTrendsHandler, UIHelper.LISTVIEW_ACTION_INIT);
								}	
							}	 
						 	else 
						 	{
								if (lvBooklistData.isEmpty()) {
									loadLvBooklistData(curBooklistCatalog, 1, lvBooklistHandler, UIHelper.LISTVIEW_ACTION_INIT);
								}	
							}						
							break;
							
						case 1:
							if (lvMyBooklistData.isEmpty()) {
								loadLvMyBooklistData(curMyBooklistCatalog, 1, lvMyBooklistHandler, UIHelper.LISTVIEW_ACTION_INIT);
							}
							break;
							
						case 2:
								// 判断登录
							/***
							if (!appContext.isLogin()) {
								if (lvMyTrends.getVisibility() == View.VISIBLE && lvMyTrendsData.isEmpty()) {
									lvMyTrends_foot_more.setText(R.string.load_empty);
									lvMyTrends_foot_progress.setVisibility(View.GONE);
								} else if (lvMyMessage.getVisibility() == View.VISIBLE && lvMyMessageData.isEmpty()) {
									lvMyMessage_foot_more.setText(R.string.load_empty);
									lvMyMessage_foot_progress.setVisibility(View.GONE);
								}
								UIHelper.showLoginDialog(Main.this);
								break;
							}
							***/
							
						
							
							if (lvMyTrendsData.isEmpty()) {
								loadLvMyTrendsData(appContext.getLoginUid(), 1, lvMyTrendsHandler, UIHelper.LISTVIEW_ACTION_INIT);
							}
							
							// 处理通知信息
							/***
							if (bv_message.isShown())
								frameMyTrendsBtnClick(framebtn_my_message, UIHelper.LISTVIEW_ACTION_REFRESH);
							else if (lvMyMessage.getVisibility() == View.VISIBLE && lvMyMessageData.isEmpty())
								loadLvMyMessageData(appContext.getLoginUid(), 1, lvMyMessageHandler, UIHelper.LISTVIEW_ACTION_INIT);
							***/
							
							if ( bv_message.isShown() )
								loadLvMyMessageData(appContext.getLoginUid(), 1, lvMyMessageHandler, UIHelper.LISTVIEW_ACTION_INIT);
							break;
						}
						setCurPoint(viewIndex);
					}
				});
	}

	/**
	 * 设置底部栏当前焦点
	 * @param index
	 */
	private void setCurPoint(int index) {
		
		if (index < 0 || index > mViewCount - 1 || mCurSel == index)
			return;

		mButtons[mCurSel].setChecked(false);
		mButtons[index].setChecked(true);
		mHeadTitle.setText(mHeadTitles[index]);
		mCurSel = index;

		mHead_search.setVisibility(View.GONE);
		mHeadPub_post.setVisibility(View.GONE);
		mHeadPub_tweet.setVisibility(View.GONE);
		
		// 头部logo、发帖、发动弹按钮显示
		if (index == 0) {
			mHeadLogo.setImageResource(R.drawable.frame_logo_news);
			mHead_search.setVisibility(View.VISIBLE);
		} else if (index == 1) {
			mHeadLogo.setImageResource(R.drawable.frame_logo_post);
			mHeadPub_post.setVisibility(View.VISIBLE);
		} else if (index == 2) {
			mHeadLogo.setImageResource(R.drawable.frame_logo_tweet);
		//	mHeadPub_tweet.setVisibility(View.VISIBLE);
		}
		
	}

	
	/**
	 * 初始化各个主页的按钮(资讯、问答、动弹、动态、留言)
	 */
	private void initFrameButton() {
		
		// 初始化按钮控件
		framebtn_trends_lastest = (Button) findViewById(R.id.frame_btn_trends_lastest);
		framebtn_hot_booklist = (Button) findViewById(R.id.frame_btn_hot_booklist);
		framebtn_zero_booklist = (Button) findViewById(R.id.frame_btn_zero_booklist);
		
		framebtn_followed_booklist = (Button) findViewById(R.id.frame_btn_followed_booklist);
		framebtn_created_booklist = (Button) findViewById(R.id.frame_btn_created_booklist);
		framebtn_part_booklist = (Button) findViewById(R.id.frame_btn_part_booklist);
		
		framebtn_my_trends = (Button) findViewById(R.id.frame_btn_my_trends);
		framebtn_my_bookshelf = (Button) findViewById(R.id.frame_btn_my_bookshelf);
		framebtn_my_message = (Button) findViewById(R.id.frame_btn_my_message);
		
		// 设置首选择项
		framebtn_trends_lastest.setEnabled(false);
		framebtn_followed_booklist.setEnabled(false);
		framebtn_my_trends.setEnabled(false);
		
		// 广场页 最新动态 + 热门书单 + 零回答书单
		framebtn_trends_lastest.setOnClickListener(
			frameTrendsBtnClick( framebtn_trends_lastest, -1 )
		);
		framebtn_hot_booklist.setOnClickListener(
			frameTrendsBtnClick( framebtn_hot_booklist, BooklistList.CATALOG_HOT_BOOKLIST )
		);
		framebtn_zero_booklist.setOnClickListener(
			frameTrendsBtnClick( framebtn_zero_booklist, BooklistList.CATALOG_ZERO_BOOKLIST )
		);
		
		// 我的书单 关注的书单 + 创建的书单 + 参与的书单
		framebtn_followed_booklist.setOnClickListener(
			frameMyBooklistBtnClick( framebtn_followed_booklist, BooklistList.CATALOG_FOLLOWED_BOOKLIST )
		);
		framebtn_created_booklist.setOnClickListener(
			frameMyBooklistBtnClick( framebtn_created_booklist, BooklistList.CATALOG_CREATED_BOOKLIST )
		);
		framebtn_part_booklist.setOnClickListener(
			frameMyBooklistBtnClick( framebtn_part_booklist, BooklistList.CATALOG_PART_BOOKLIST )
		);
		
		// 我的空间 我的动态  + 我的书架 + 我的消息
		framebtn_my_trends.setOnClickListener(frameMyTrendsBtnClick(framebtn_my_trends, 0));
		
		framebtn_my_bookshelf.setOnClickListener(frameMyTrendsBtnClick(framebtn_my_bookshelf, 74));
		
		framebtn_my_message.setOnClickListener(frameMyTrendsBtnClick(framebtn_my_message, 74));
		
	}

	private View.OnClickListener frameTrendsBtnClick(final Button btn, final int catalog) {
		return new View.OnClickListener() {
			public void onClick(View v) {
				if (btn == framebtn_trends_lastest) {
					framebtn_trends_lastest.setEnabled(false);
				} else {
					framebtn_trends_lastest.setEnabled(true);
				}
				if (btn == framebtn_hot_booklist) {
					framebtn_hot_booklist.setEnabled(false);
				} else {
					framebtn_hot_booklist.setEnabled(true);
				}
				if (btn == framebtn_zero_booklist) {
					framebtn_zero_booklist.setEnabled(false);
				} else {
					framebtn_zero_booklist.setEnabled(true);
				}

				if (btn == framebtn_trends_lastest) {
					curTrendsCatalog = catalog;
					lvRecentTrends.setVisibility(View.VISIBLE);
					lvBooklist.setVisibility(View.GONE);
					loadLvTrendsData(curTrendsCatalog, 1, lvRecentTrendsHandler, UIHelper.LISTVIEW_ACTION_CHANGE_CATALOG);
				} else {		
					curBooklistCatalog = catalog;
					lvRecentTrends.setVisibility(View.GONE);
					lvBooklist.setVisibility(View.VISIBLE);
					loadLvBooklistData(curBooklistCatalog, 1, lvBooklistHandler, UIHelper.LISTVIEW_ACTION_CHANGE_CATALOG);
				}
			}
		};
	}
	
	private View.OnClickListener frameMyBooklistBtnClick(final Button btn,	final int catalog) {
		return new View.OnClickListener() {
			public void onClick(View v) {
				if (btn == framebtn_followed_booklist) {
					framebtn_followed_booklist.setEnabled(false);
				} else {
					framebtn_followed_booklist.setEnabled(true);
				}
				if (btn == framebtn_created_booklist) {
					framebtn_created_booklist.setEnabled(false);
				} else {
					framebtn_created_booklist.setEnabled(true);
				}
				if (btn == framebtn_part_booklist) {
					framebtn_part_booklist.setEnabled(false);
				} else {
					framebtn_part_booklist.setEnabled(true);
				}

				curMyBooklistCatalog = catalog;
				loadLvMyBooklistData(curMyBooklistCatalog, 1, lvMyBooklistHandler, UIHelper.LISTVIEW_ACTION_CHANGE_CATALOG);
			}
		};
	}

	private View.OnClickListener frameMyTrendsBtnClick(final Button btn, final int catalog) {
		return new View.OnClickListener() {
			public void onClick(View v) {
				if (btn == framebtn_my_trends)
					framebtn_my_trends.setEnabled(false);
				else
					framebtn_my_trends.setEnabled(true);
				
				if (btn == framebtn_my_bookshelf)
					framebtn_my_bookshelf.setEnabled(false);
				else
					framebtn_my_bookshelf.setEnabled(true);
				
				if (btn == framebtn_my_message)
					framebtn_my_message.setEnabled(false);
				else
					framebtn_my_message.setEnabled(true);
				
				
				if(btn == framebtn_my_message && bv_message.isShown()) {
					int action = UIHelper.LISTVIEW_ACTION_REFRESH;
					isClearNotice = true;
					curClearNoticeType = Notice.TYPE_MESSAGE;
				}

				if (btn == framebtn_my_trends) {	
					lvMyTrends.setVisibility(View.VISIBLE);
					lvMyBookshelf.setVisibility(View.GONE);
					lvMyMessage.setVisibility(View.GONE);
					loadLvMyTrendsData(appContext.getLoginUid(), 1, lvMyTrendsHandler, UIHelper.LISTVIEW_ACTION_CHANGE_CATALOG);	
				}
				
				if (btn == framebtn_my_bookshelf) {
					lvMyTrends.setVisibility(View.GONE);
					lvMyBookshelf.setVisibility(View.VISIBLE);
					lvMyMessage.setVisibility(View.GONE);

					loadLvMyBookshelfData(appContext.getLoginUid(), 1, lvMyBookshelfHandler, UIHelper.LISTVIEW_ACTION_CHANGE_CATALOG);
				}
				
				if (btn == framebtn_my_message) {
					lvMyTrends.setVisibility(View.GONE);
					lvMyBookshelf.setVisibility(View.GONE);
					lvMyMessage.setVisibility(View.VISIBLE);
					curMyMessageCatalog = 74;
					loadLvMyMessageData(appContext.getLoginUid(), 1, lvMyMessageHandler, UIHelper.LISTVIEW_ACTION_CHANGE_CATALOG);
				}
					
			}
		};
	}

	/**
	 * 初始化快捷栏
	 */
	private void initQuickActionGrid() {
		mGrid = new QuickActionGrid(this);	
		mGrid.addQuickAction(new MyQuickAction(this, R.drawable.ic_menu_search, R.string.main_menu_search));
		mGrid.addQuickAction(new MyQuickAction(this, R.drawable.ic_menu_myinfo, R.string.main_menu_booklistpub));
		mGrid.addQuickAction(new MyQuickAction(this, R.drawable.ic_menu_software, R.string.main_menu_new_book));
		mGrid.addQuickAction(new MyQuickAction(this, R.drawable.ic_menu_login, R.string.main_menu_myinfo));
		mGrid.addQuickAction(new MyQuickAction(this, R.drawable.ic_menu_setting, R.string.main_menu_setting));
		mGrid.addQuickAction(new MyQuickAction(this, R.drawable.ic_menu_exit, R.string.main_menu_exit));
		mGrid.setOnQuickActionClickListener(mActionListener);
	}
	
	/**
	 * 快捷栏item点击事件
	 */
	private OnQuickActionClickListener mActionListener = new OnQuickActionClickListener() {
		public void onQuickActionClicked(QuickActionWidget widget, int position) {
			switch (position) {
			case QUICKACTION_SEARCH:// 搜索
				UIHelper.showSearch(Main.this);
				break;
			case QUICKACTION_PUB_BOOKLIST:// 荐书
				UIHelper.showBooklistPub(Main.this);
				break;
				
			case QUICKACTION_NEW_BOOK:// 新书
				UIHelper.showNewbook( Main.this );
				break;

			case QUICKACTION_USERINFO:// 我
				UIHelper.showUserInfo(Main.this);
				break;

			case QUICKACTION_SETTING:// 设置
				UIHelper.showSetting(Main.this);
				break;
				
			case QUICKACTION_EXIT:// 退出
				UIHelper.Exit(Main.this);
				break;
			}
		}
	};
	
	/**
	 * 初始化所有ListView
	 */
	private void initFrameListView() {
		// 初始化listview控件
		this.initRecentTrendsListView();
		this.initBooklistListView();
		this.initMyBooklistListView();
		this.initMyTrendsListView();
		this.initMyBookshelfListView();
		this.initMyMessageListView();
		
		// 加载listview数据
		this.initFrameListViewData();
	}
	
	/**
	 * 初始化最新动态列表
	 */
	private void initRecentTrendsListView() {
		
		lvRecentTrendsAdapter = new ListViewRecentTrendsAdapter(this, lvRecentTrendsData, R.layout.recent_trends_listitem, true);
		lvTrends_footer = getLayoutInflater().inflate(R.layout.listview_footer,	null);
		lvTrends_foot_more = (TextView) lvTrends_footer.findViewById(R.id.listview_foot_more);
		lvTrends_foot_progress = (ProgressBar) lvTrends_footer.findViewById(R.id.listview_foot_progress);
		lvRecentTrends = (PullToRefreshListView) findViewById(R.id.frame_listview_trends);
		lvRecentTrends.addFooterView(lvTrends_footer);// 添加底部视图 必须在setAdapter前
		lvRecentTrends.setAdapter(lvRecentTrendsAdapter);
		
		lvRecentTrends.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				
				// 点击头部、底部栏无效
				if (position == 0 || view == lvTrends_footer)
					return;

				Behavior behavior = null;
				// 判断是否是TextView
				if (view instanceof TextView) {
					behavior = (Behavior) view.getTag();
				} else {
					TextView tv = (TextView) view.findViewById(R.id.recent_trends_listitem_content);
					behavior = (Behavior) tv.getTag();
				}
				if (behavior == null)
					return;

				// 跳转到新闻详情
				//UIHelper.showBooklistDetail(view.getContext(), booklist.getId());
			}
		});
		
		lvRecentTrends.setOnScrollListener(new AbsListView.OnScrollListener() {
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				lvRecentTrends.onScrollStateChanged(view, scrollState);

				// 数据为空--不用继续下面代码了
				if (lvRecentTrendsData.isEmpty())
					return;

				// 判断是否滚动到底部
				boolean scrollEnd = false;
				try {
					if (view.getPositionForView(lvTrends_footer) == view.getLastVisiblePosition())
						scrollEnd = true;
				} catch (Exception e) {
					scrollEnd = false;
				}

				int lvDataState = StringUtils.toInt(lvRecentTrends.getTag());
				if (scrollEnd && lvDataState == UIHelper.LISTVIEW_DATA_MORE) {
					lvRecentTrends.setTag(UIHelper.LISTVIEW_DATA_LOADING);
					lvTrends_foot_more.setText(R.string.load_ing);
					lvTrends_foot_progress.setVisibility(View.VISIBLE);
					
					// 当前pageIndex
					int pageIndex = lvRecentTrendsSumData / AppContext.PAGE_SIZE;
					loadLvTrendsData(curTrendsCatalog, pageIndex, lvRecentTrendsHandler, UIHelper.LISTVIEW_ACTION_SCROLL);
				}
			}

			public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
				lvRecentTrends.onScroll(view, firstVisibleItem, visibleItemCount,	totalItemCount);
			}
		});
		
		lvRecentTrends.setOnRefreshListener(new PullToRefreshListView.OnRefreshListener() {
			public void onRefresh() {
				loadLvTrendsData(curTrendsCatalog, 1, lvRecentTrendsHandler, UIHelper.LISTVIEW_ACTION_REFRESH);
			}
		});
		
	}
	
	/**
	 * 初始化热门书单列表
	 */
	private void initBooklistListView() {
		
		lvBooklistAdapter = new ListViewBooklistAdapter(this, BooklistList.CATALOG_HOT_BOOKLIST, lvBooklistData, R.layout.booklist_listitem);
		
		lvBooklist_footer = getLayoutInflater().inflate(R.layout.listview_footer, null);
		lvBooklist_foot_more = (TextView) lvBooklist_footer.findViewById(R.id.listview_foot_more);
		lvBooklist_foot_progress = (ProgressBar) lvBooklist_footer.findViewById(R.id.listview_foot_progress);
		lvBooklist = (PullToRefreshListView) findViewById(R.id.frame_listview_booklist);
		lvBooklist.addFooterView(lvBooklist_footer);// 添加底部视图 必须在setAdapter前
		lvBooklist.setAdapter(lvBooklistAdapter);
		
		lvBooklist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				// 点击头部、底部栏无效
				if (position == 0 || view == lvBooklist_footer)
					return;

				Booklist booklist = null;
				// 判断是否是TextView
				if (view instanceof TextView) {
					booklist = (Booklist) view.getTag();
				} else {
					TextView tv = (TextView) view.findViewById(R.id.booklist_listitem_title);
					booklist = (Booklist) tv.getTag();
				}
				if (booklist == null)
					return;

				// 跳转书单详情页
				UIHelper.showBooklistDetail(view.getContext(), booklist.getId());
			}
		});
		
		lvBooklist.setOnScrollListener(new AbsListView.OnScrollListener() {
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				lvBooklist.onScrollStateChanged(view, scrollState);

				// 数据为空--不用继续下面代码了
				if (lvBooklistData.isEmpty())
					return;

				// 判断是否滚动到底部
				boolean scrollEnd = false;
				try {
					if (view.getPositionForView(lvBooklist_footer) == view.getLastVisiblePosition())
						scrollEnd = true;
				} catch (Exception e) {
					scrollEnd = false;
				}

				int lvDataState = StringUtils.toInt(lvBooklist.getTag());
				if (scrollEnd && lvDataState == UIHelper.LISTVIEW_DATA_MORE) {
					lvBooklist.setTag(UIHelper.LISTVIEW_DATA_LOADING);
					lvBooklist_foot_more.setText(R.string.load_ing);
					lvBooklist_foot_progress.setVisibility(View.VISIBLE);
					// 当前pageIndex
					int pageIndex = lvBooklistSumData / AppContext.PAGE_SIZE;
					loadLvBooklistData(curBooklistCatalog, pageIndex, lvBooklistHandler, UIHelper.LISTVIEW_ACTION_SCROLL);
				}
			}

			public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
				lvBooklist.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);
			}
		});
		
		lvBooklist.setOnRefreshListener(new PullToRefreshListView.OnRefreshListener() {
			public void onRefresh() {
				loadLvBooklistData(curBooklistCatalog, 1, lvBooklistHandler, UIHelper.LISTVIEW_ACTION_REFRESH);
			}
		});
		
	}
	
	/**
	 * 初始化我的书单列表
	 */
	private void initMyBooklistListView() {
		
		lvMyBooklistAdapter = new ListViewBooklistAdapter(this, BooklistList.CATALOG_FOLLOWED_BOOKLIST, lvMyBooklistData, R.layout.booklist_listitem);
		
		lvMyBooklist_footer = getLayoutInflater().inflate(R.layout.listview_footer, null);
		lvMyBooklist_foot_more = (TextView) lvMyBooklist_footer.findViewById(R.id.listview_foot_more);
		lvMyBooklist_foot_progress = (ProgressBar) lvMyBooklist_footer.findViewById(R.id.listview_foot_progress);
		
		lvMyBooklist = (PullToRefreshListView) findViewById(R.id.frame_listview_mybooklist);
		lvMyBooklist.addFooterView(lvMyBooklist_footer);// 添加底部视图 必须在setAdapter前
		lvMyBooklist.setAdapter(lvMyBooklistAdapter);
		
		lvMyBooklist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				// 点击头部、底部栏无效
				if (position == 0 || view == lvMyBooklist_footer)
					return;

				Booklist booklist = null;
				// 判断是否是TextView
				if (view instanceof TextView) {
					booklist = (Booklist) view.getTag();
				} else {
					TextView tv = (TextView) view.findViewById(R.id.booklist_listitem_title);
					booklist = (Booklist) tv.getTag();
				}
				if (booklist == null)
					return;

				// 跳转书单详情页
				UIHelper.showBooklistDetail(view.getContext(), booklist.getId());

			}
		});
		
		lvMyBooklist.setOnScrollListener(new AbsListView.OnScrollListener() {
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				lvMyBooklist.onScrollStateChanged(view, scrollState);

				// 数据为空--不用继续下面代码了
				if (lvMyBooklistData.isEmpty())
					return;

				// 判断是否滚动到底部
				boolean scrollEnd = false;
				try {
					if (view.getPositionForView(lvMyBooklist_footer) == view.getLastVisiblePosition())
						scrollEnd = true;
				} catch (Exception e) {
					scrollEnd = false;
				}

				int lvDataState = StringUtils.toInt(lvMyBooklist.getTag());
				if (scrollEnd && lvDataState == UIHelper.LISTVIEW_DATA_MORE) {
					lvMyBooklist.setTag(UIHelper.LISTVIEW_DATA_LOADING);
					lvMyBooklist_foot_more.setText(R.string.load_ing);
					lvMyBooklist_foot_progress.setVisibility(View.VISIBLE);
					// 当前pageIndex
					int pageIndex = lvMyBooklistSumData / AppContext.PAGE_SIZE;
					loadLvMyBooklistData(curMyBooklistCatalog, pageIndex, lvMyBooklistHandler, UIHelper.LISTVIEW_ACTION_SCROLL);
							
				}
			}

			public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {	
				lvMyBooklist.onScroll(view, firstVisibleItem, visibleItemCount,totalItemCount);			
			}
		});
		
		lvMyBooklist.setOnRefreshListener(new PullToRefreshListView.OnRefreshListener() {
					public void onRefresh() {
						loadLvMyBooklistData(curMyBooklistCatalog, 1, lvMyBooklistHandler, UIHelper.LISTVIEW_ACTION_REFRESH);
								
					}
			});
	}
	
	/**
	 * 初始化我的动态列表
	 */
	private void initMyTrendsListView() {
		
		lvMyTrendsAdapter = new ListViewMyTrendsAdapter(this, lvMyTrendsData, R.layout.recent_trends_listitem);	
		lvMyTrends_footer = getLayoutInflater().inflate(R.layout.listview_footer,	null);
		lvMyTrends_foot_more = (TextView) lvMyTrends_footer.findViewById(R.id.listview_foot_more);
		lvMyTrends_foot_progress = (ProgressBar) lvMyTrends_footer.findViewById(R.id.listview_foot_progress);
		lvMyTrends = (PullToRefreshListView) findViewById(R.id.frame_listview_my_trends);
		lvMyTrends.addFooterView(lvMyTrends_footer);// 添加底部视图 必须在setAdapter前
		lvMyTrends.setAdapter(lvMyTrendsAdapter);

		lvMyTrends.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				
			
				// 点击头部、底部栏无效
				if (position == 0 || view == lvMyTrends_footer)
					return;

				Behavior behavior = null;
				// 判断是否是TextView
				if (view instanceof TextView) {
					behavior = (Behavior) view.getTag();
				} else {
					TextView tv = (TextView) view.findViewById(R.id.recent_trends_listitem_content);
					behavior = (Behavior) tv.getTag();
				}
				if (behavior == null)
					return;

				// 跳转到新闻详情
				//UIHelper.showBooklistDetail(view.getContext(), booklist.getId());
			}
		});
		
		lvMyTrends.setOnScrollListener(new AbsListView.OnScrollListener() {
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				lvMyTrends.onScrollStateChanged(view, scrollState);

				// 数据为空--不用继续下面代码了
				if (lvMyTrendsData.isEmpty())
					return;

				// 判断是否滚动到底部
				boolean scrollEnd = false;
				try {
					if (view.getPositionForView(lvMyTrends_footer) == view.getLastVisiblePosition())
						scrollEnd = true;
				} catch (Exception e) {
					scrollEnd = false;
				}

				int lvDataState = StringUtils.toInt(lvMyTrends.getTag());
				if (scrollEnd && lvDataState == UIHelper.LISTVIEW_DATA_MORE) {
					lvMyTrends.setTag(UIHelper.LISTVIEW_DATA_LOADING);
					lvMyTrends_foot_more.setText(R.string.load_ing);
					lvMyTrends_foot_progress.setVisibility(View.VISIBLE);
					// 当前pageIndex
					int pageIndex = lvMyTrendsSumData / AppContext.PAGE_SIZE;
					loadLvMyTrendsData(appContext.getLoginUid(), pageIndex, lvMyTrendsHandler, UIHelper.LISTVIEW_ACTION_SCROLL);
				}
			}

			public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
				lvMyTrends.onScroll(view, firstVisibleItem, visibleItemCount,	totalItemCount);
			}
		});
		
		lvMyTrends.setOnRefreshListener(new PullToRefreshListView.OnRefreshListener() {
			public void onRefresh() {
				loadLvMyTrendsData(appContext.getLoginUid(), 1, lvMyTrendsHandler, UIHelper.LISTVIEW_ACTION_REFRESH);
			}
		});
		
	}

	/**
	 * 初始化我的藏书架
	 */
	private void initMyBookshelfListView() {
		
		lvMyBookshelfAdapter = new ListViewBookshelfAdapter(this, lvMyBookshelfData, R.layout.bookshelf_listitem);
		
		lvMyBookshelf_footer = getLayoutInflater().inflate(R.layout.listview_footer, null);
		lvMyBookshelf_foot_more = (TextView) lvMyBookshelf_footer.findViewById(R.id.listview_foot_more);
		lvMyBookshelf_foot_progress = (ProgressBar) lvMyBookshelf_footer.findViewById(R.id.listview_foot_progress);
		
		lvMyBookshelf = (PullToRefreshListView) findViewById(R.id.frame_listview_my_bookshelf);
		lvMyBookshelf.addFooterView(lvMyBookshelf_footer);// 添加底部视图 必须在setAdapter前
		lvMyBookshelf.setAdapter(lvMyBookshelfAdapter);
		
		lvMyBookshelf.setOnItemClickListener(new AdapterView.OnItemClickListener() {
					public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
						// 点击头部、底部栏无效
						if (position == 0 || view == lvMyBookshelf_footer)
							return;

						Bookshelf bookshelf = null;
						// 判断是否是TextView
						if (view instanceof TextView) {
							bookshelf = (Bookshelf) view.getTag();
						} else {
							TextView tv = (TextView) view.findViewById(R.id.bookshelf_listitem_title);
							bookshelf = (Bookshelf) tv.getTag();
						}
						if (bookshelf == null)
							return;

						// 跳转到问答详情
					//	UIHelper.showBooklistDetail(view.getContext(), bookshelf.getId() );
					}
		});
		
		lvMyBookshelf.setOnScrollListener(new AbsListView.OnScrollListener() {
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				lvMyBookshelf.onScrollStateChanged(view, scrollState);

				// 数据为空--不用继续下面代码了
				if (lvMyBookshelfData.isEmpty())
					return;

				// 判断是否滚动到底部
				boolean scrollEnd = false;
				try {
					if (view.getPositionForView(lvMyBookshelf_footer) == view.getLastVisiblePosition())
						scrollEnd = true;
				} catch (Exception e) {
					scrollEnd = false;
				}

				int lvDataState = StringUtils.toInt(lvMyBookshelf.getTag());
				if (scrollEnd && lvDataState == UIHelper.LISTVIEW_DATA_MORE) {
					lvMyBookshelf.setTag(UIHelper.LISTVIEW_DATA_LOADING);
					lvMyBookshelf_foot_more.setText(R.string.load_ing);
					lvMyBookshelf_foot_progress.setVisibility(View.VISIBLE);
					// 当前pageIndex
					int pageIndex = lvMyBookshelfSumData / AppContext.PAGE_SIZE;
					loadLvMyBookshelfData(appContext.getLoginUid(), pageIndex, lvMyBookshelfHandler, UIHelper.LISTVIEW_ACTION_SCROLL);
							
				}
			}

			public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {	
				lvMyBookshelf.onScroll(view, firstVisibleItem, visibleItemCount,totalItemCount);			
			}
		});
		
		lvMyBookshelf.setOnRefreshListener(new PullToRefreshListView.OnRefreshListener() {
					public void onRefresh() {
						loadLvMyBookshelfData(appContext.getLoginUid(), 1, lvMyBookshelfHandler, UIHelper.LISTVIEW_ACTION_REFRESH);
					}
		});
	}
	
	private void initMyMessageListView() {
		
		lvMyMessageAdapter = new ListViewMyTrendsAdapter(this, lvMyMessageData, R.layout.recent_trends_listitem);	
		lvMyMessage_footer = getLayoutInflater().inflate(R.layout.listview_footer,	null);
		lvMyMessage_foot_more = (TextView) lvMyMessage_footer.findViewById(R.id.listview_foot_more);
		lvMyMessage_foot_progress = (ProgressBar) lvMyMessage_footer.findViewById(R.id.listview_foot_progress);
		lvMyMessage = (PullToRefreshListView) findViewById(R.id.frame_listview_message);
		lvMyMessage.addFooterView(lvMyMessage_footer);// 添加底部视图 必须在setAdapter前
		lvMyMessage.setAdapter(lvMyMessageAdapter);

		lvMyMessage.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

				// 点击头部、底部栏无效
				if (position == 0 || view == lvMyMessage_footer)
					return;

				Behavior behavior = null;
				// 判断是否是TextView
				if (view instanceof TextView) {
					behavior = (Behavior) view.getTag();
				} else {
					TextView tv = (TextView) view.findViewById(R.id.recent_trends_listitem_content);
					behavior = (Behavior) tv.getTag();
				}
				if (behavior == null)
					return;

				// 跳转到通知详情
				//UIHelper.showMessageDetail(view.getContext(), msg.getFriendId(), msg.getFriendName());
			}
		});
		
		lvMyMessage.setOnScrollListener(new AbsListView.OnScrollListener() {
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				lvMyMessage.onScrollStateChanged(view, scrollState);

				// 数据为空--不用继续下面代码了
				if (lvMyMessageData.isEmpty())
					return;

				// 判断是否滚动到底部
				boolean scrollEnd = false;
				try {
					if (view.getPositionForView(lvMyMessage_footer) == view.getLastVisiblePosition())
						scrollEnd = true;
				} catch (Exception e) {
					scrollEnd = false;
				}

				int lvDataState = StringUtils.toInt(lvMyMessage.getTag());
				if (scrollEnd && lvDataState == UIHelper.LISTVIEW_DATA_MORE) {
					lvMyMessage.setTag(UIHelper.LISTVIEW_DATA_LOADING);
					lvMyMessage_foot_more.setText(R.string.load_ing);
					lvMyMessage_foot_progress.setVisibility(View.VISIBLE);
					// 当前pageIndex
					int pageIndex = lvMyMessageSumData / AppContext.PAGE_SIZE;
					loadLvMyMessageData(appContext.getLoginUid(), pageIndex, lvMyMessageHandler, UIHelper.LISTVIEW_ACTION_SCROLL);
				}
			}

			public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
				lvMyMessage.onScroll(view, firstVisibleItem, visibleItemCount,	totalItemCount);
			}
		});
		
		lvMyMessage.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					int position, long id) {
				// 点击头部、底部栏无效
				if (position == 0 || view == lvMyMessage_footer)
					return false;

				Behavior behavior = null;
				// 判断是否是TextView
				if (view instanceof TextView) {
					behavior = (Behavior) view.getTag();
				} else {
					TextView tv = (TextView) view.findViewById(R.id.recent_trends_listitem_content);
					behavior = (Behavior) tv.getTag();
				}
				if (behavior == null)
					return false;

				final Behavior message = behavior;

				// 选择操作
				final Handler handler = new Handler() {
					public void handleMessage(Message msg) {
						if (msg.what == 1) {
							MyResult res = (MyResult) msg.obj;
							if (res.OK()) {
								lvMyMessageData.remove(message);
								lvMyMessageAdapter.notifyDataSetChanged();
							}
							UIHelper.ToastMessage(Main.this, res.getErrorMessage());
						} else {
							((AppException) msg.obj).makeToast(Main.this);
						}
					}
				};
				
				Thread thread = new Thread() {
					public void run() {
						Message msg = new Message();
						try {
							MyResult res = appContext.delMessage(	appContext.getLoginUid() );
							msg.what = 1;
							msg.obj = res;
						} catch (AppException e) {
							e.printStackTrace();
							msg.what = -1;
							msg.obj = e;
						}
						handler.sendMessage(msg);
					}
				};
				
				UIHelper.showMessageListOptionDialog(Main.this, message, thread);
				
				return true;
			}
		});

		
		lvMyMessage.setOnRefreshListener(new PullToRefreshListView.OnRefreshListener() {
			public void onRefresh() {
				// 清除通知信息
				if (bv_message.isShown()) {
					isClearNotice = true;
					curClearNoticeType = Notice.TYPE_MESSAGE;
				}
				
				// 刷新数据
				loadLvMyMessageData(appContext.getLoginUid(), 1, lvMyMessageHandler, UIHelper.LISTVIEW_ACTION_REFRESH);
			}
		});
		
	}

	/**
	 * 初始化所有ListView数据
	 */
	private void initFrameListViewData() {
		// 初始化Handler
		
		lvRecentTrendsHandler = this.getLvHandler(lvRecentTrends, lvRecentTrendsAdapter,
				lvTrends_foot_more, lvTrends_foot_progress, AppContext.PAGE_SIZE);
		
		
		lvBooklistHandler = this.getLvHandler(lvBooklist, lvBooklistAdapter,
				lvBooklist_foot_more, lvBooklist_foot_progress, AppContext.PAGE_SIZE);
		
		lvMyBooklistHandler = this.getLvHandler(lvMyBooklist, lvMyBooklistAdapter,
				lvMyBooklist_foot_more, lvMyBooklist_foot_progress, AppContext.PAGE_SIZE);
		
		
		lvMyTrendsHandler = this.getLvHandler(lvMyTrends, lvMyTrendsAdapter,
				lvMyTrends_foot_more, lvMyTrends_foot_progress, AppContext.PAGE_SIZE);
		
		lvMyBookshelfHandler = this.getLvHandler(lvMyBookshelf, lvMyBookshelfAdapter,
				lvMyBookshelf_foot_more, lvMyBookshelf_foot_progress, AppContext.PAGE_SIZE);
		

		lvMyMessageHandler = this.getLvHandler(lvMyMessage, lvMyMessageAdapter, lvMyMessage_foot_more,
				lvMyMessage_foot_progress, AppContext.PAGE_SIZE);
		
		// 加载最新动态数据
		if (lvRecentTrendsData.isEmpty()) {
			loadLvTrendsData(curTrendsCatalog, 1, lvRecentTrendsHandler, UIHelper.LISTVIEW_ACTION_INIT);
		}
	}
	
	/**
	 * 获取listview的初始化Handler
	 * 
	 * @param lv
	 * @param adapter
	 * @return
	 */
	private Handler getLvHandler(final PullToRefreshListView lv, 
			final BaseAdapter adapter, final TextView more,
			final ProgressBar progress, final int pageSize) {
		
		return new Handler() {

			public void handleMessage(Message msg) {
				if (msg.what >= 0) {
					
					// listview数据处理
					Notice notice = handleLvData(msg.what, msg.obj, msg.arg2, msg.arg1);
					
					if (msg.what < pageSize) {
						
						lv.setTag(UIHelper.LISTVIEW_DATA_FULL);
						adapter.notifyDataSetChanged();
						more.setText(R.string.load_full);
						
					} else if (msg.what == pageSize) {
						
						lv.setTag(UIHelper.LISTVIEW_DATA_MORE);
						adapter.notifyDataSetChanged();
						more.setText(R.string.load_more);

						// 特殊处理-热门动弹不能翻页
						/***
						if (lv == lvTweet) {
							TweetList tlist = (TweetList) msg.obj;
							if (lvTweetData.size() == tlist.getTweetCount()) {
								lv.setTag(UIHelper.LISTVIEW_DATA_FULL);
								more.setText(R.string.load_full);
							}
						}
						***/
					}
					
					// 发送通知广播
					if (notice != null) {
						UIHelper.sendBroadCast(lv.getContext(), notice);
					}
					
					// 是否清除通知信息
					if (isClearNotice) {
						ClearNotice(curClearNoticeType);
						isClearNotice = false;// 重置
						curClearNoticeType = 0;
					}
					
				} else if (msg.what == -1) {
					
					// 有异常--显示加载出错 & 弹出错误消息
					lv.setTag(UIHelper.LISTVIEW_DATA_MORE);
					more.setText(R.string.load_error);
					((AppException) msg.obj).makeToast(Main.this);
					
				}
				
				if (adapter.getCount() == 0) {
					lv.setTag(UIHelper.LISTVIEW_DATA_EMPTY);
					more.setText(R.string.load_empty);
				}
				
				progress.setVisibility(ProgressBar.GONE);
				mHeadProgress.setVisibility(ProgressBar.GONE);
				
				if (msg.arg1 == UIHelper.LISTVIEW_ACTION_REFRESH) {
					
					lv.onRefreshComplete( getString(R.string.pull_to_refresh_update) + new Date().toLocaleString());
					lv.setSelection(0);
					
				} else if (msg.arg1 == UIHelper.LISTVIEW_ACTION_CHANGE_CATALOG) {
					
					lv.onRefreshComplete();
					lv.setSelection(0);
					
				}
			}
		};
	}

	/**
	 * listview数据处理
	 * 
	 * @param what 			数量
	 * @param obj  			数据
	 * @param objtype		数据类型
	 * @param actiontype	操作类型
	 * @return notice 		通知信息
	 */
	private Notice handleLvData(int what, Object obj, int objtype, int actiontype) {
		Notice notice = null;
		switch (actiontype) {
		case UIHelper.LISTVIEW_ACTION_INIT:
		case UIHelper.LISTVIEW_ACTION_REFRESH:
		case UIHelper.LISTVIEW_ACTION_CHANGE_CATALOG:
			int newdata = 0;// 新加载数据-只有刷新动作才会使用到
			switch (objtype) {
			case UIHelper.LISTVIEW_DATATYPE_TRENDS:
				
				BehaviorList nlist = (BehaviorList) obj;
				notice = nlist.getNotice();
				lvRecentTrendsSumData = what;
				if (actiontype == UIHelper.LISTVIEW_ACTION_REFRESH) {
					if (lvRecentTrendsData.size() > 0) {
						for (Behavior behavior1 : nlist.getBehaviorList()) {
							boolean b = false;
							for (Behavior behavior2 : lvRecentTrendsData) {
								if (behavior1.getId() == behavior2.getId()) {
									b = true;
									break;
								}
							}
							if (!b)
								newdata++;
						}
					} else {
						newdata = what;
					}
				}
				lvRecentTrendsData.clear();// 先清除原有数据
				lvRecentTrendsData.addAll( nlist.getBehaviorList() );
				break;
				
			case UIHelper.LISTVIEW_DATATYPE_BOOKLIST:
				
				BooklistList blist = (BooklistList) obj;
				notice = blist.getNotice();
				lvBooklistSumData = what;
				if (actiontype == UIHelper.LISTVIEW_ACTION_REFRESH) {
					if (lvBooklistData.size() > 0) {
						for (Booklist booklist1 : blist.getBooklistList()) {
							boolean b = false;
							for (Booklist booklist2 : lvBooklistData) {
								if (booklist1.getId() == booklist2.getId()) {
									b = true;
									break;
								}
							}
							if (!b)
								newdata++;
						}
					} else {
						newdata = what;
					}
				}
				lvBooklistData.clear();// 先清除原有数据
				lvBooklistData.addAll( blist.getBooklistList() );
				break;
				
			case UIHelper.LISTVIEW_DATATYPE_MYBOOKLIST:
				
				BooklistList plist = (BooklistList) obj;
				notice = plist.getNotice();
				lvMyBooklistSumData = what;
				if (actiontype == UIHelper.LISTVIEW_ACTION_REFRESH) {
					if (lvMyBooklistData.size() > 0) {
						for (Booklist booklist1 : plist.getBooklistList()) {
							boolean b = false;
							for (Booklist booklist2 : lvMyBooklistData) {
								if (booklist1.getId() == booklist2.getId()) {
									b = true;
									break;
								}
							}
							if (!b)
								newdata++;
						}
					} else {
						newdata = what;
					}
				}
				lvMyBooklistData.clear();// 先清除原有数据
				lvMyBooklistData.addAll(plist.getBooklistList());
				break;
				
			case UIHelper.LISTVIEW_DATATYPE_MYTRENDS:
				
				BehaviorList tlist = (BehaviorList) obj;
				notice = tlist.getNotice();
				lvMyTrendsSumData = what;
				if (actiontype == UIHelper.LISTVIEW_ACTION_REFRESH) {
					if (lvMyTrendsData.size() > 0) {
						for (Behavior behavior1 : tlist.getBehaviorList()) {
							boolean b = false;
							for (Behavior behavior2 : lvMyTrendsData) {
								if (behavior1.getId() == behavior2.getId()) {
									b = true;
									break;
								}
							}
							if (!b)
								newdata++;
						}
					} else {
						newdata = what;
					}
				}
				
				lvMyTrendsData.clear();// 先清除原有数据
				lvMyTrendsData.addAll( tlist.getBehaviorList() );
				break;

			case UIHelper.LISTVIEW_DATATYPE_MYBOOKSHELF:
				
				BookshelfList alist = (BookshelfList) obj;
				notice = alist.getNotice();
				lvMyBookshelfSumData = what;
				if (actiontype == UIHelper.LISTVIEW_ACTION_REFRESH) {
					if (lvMyBookshelfData.size() > 0) {
						for (Bookshelf bookshelf1 : alist.getBookshelfList()) {
							boolean b = false;
							for (Bookshelf bookshelf2 : lvMyBookshelfData) {
								if (bookshelf1.getId() == bookshelf2.getId()) {
									b = true;
									break;
								}
							}
							if (!b)
								newdata++;
						}
					} else {
						newdata = what;
					}
				}
				
				lvMyBookshelfData.clear();// 先清除原有数据
				lvMyBookshelfData.addAll(alist.getBookshelfList());
				
				break;
				
			case UIHelper.LISTVIEW_DATATYPE_MESSAGE:
				
				BehaviorList mlist = (BehaviorList) obj;
				notice = mlist.getNotice();
				lvMyMessageSumData = what;
				if (actiontype == UIHelper.LISTVIEW_ACTION_REFRESH) {
					if (lvMyMessageData.size() > 0) {
						for (Behavior behavior1 : mlist.getBehaviorList()) {
							boolean b = false;
							for (Behavior behavior2 : lvMyMessageData) {
								if (behavior1.getId() == behavior2.getId()) {
									b = true;
									break;
								}
							}
							if (!b)
								newdata++;
						}
					} else {
						newdata = what;
					}
				}
				lvMyMessageData.clear();// 先清除原有数据
				lvMyMessageData.addAll(mlist.getBehaviorList());
				
				break;
			}
			
			if (actiontype == UIHelper.LISTVIEW_ACTION_REFRESH) {
				// 提示新加载数据
				if (newdata > 0) {
					NewDataToast.makeText( this, getString(R.string.new_data_toast_message, newdata), appContext.isAppSound()).show();
				} else {
					NewDataToast.makeText(this, getString(R.string.new_data_toast_none), false).show();
				}
			}
			break;
			
		case UIHelper.LISTVIEW_ACTION_SCROLL:
			switch (objtype) {
			case UIHelper.LISTVIEW_DATATYPE_TRENDS:
				BehaviorList list = (BehaviorList) obj;
				notice = list.getNotice();
				lvRecentTrendsSumData += what;
				if (lvRecentTrendsData.size() > 0) {
					for (Behavior behavior1 : list.getBehaviorList()) {
						boolean b = false;
						for (Behavior behavior2 : lvRecentTrendsData) {
							if (behavior1.getId() == behavior2.getId()) {
								b = true;
								break;
							}
						}
						if (!b)
							lvRecentTrendsData.add(behavior1);
					}
				} else {
					lvRecentTrendsData.addAll( list.getBehaviorList() );
				}
				break;

			case UIHelper.LISTVIEW_DATATYPE_BOOKLIST:
				
				BooklistList blist = (BooklistList) obj;
				notice = blist.getNotice();
				lvBooklistSumData += what;
				if (lvBooklistData.size() > 0) {
					for (Booklist booklist1 : blist.getBooklistList()) {
						boolean b = false;
						for (Booklist booklist2 : lvBooklistData) {
							if (booklist1.getId() == booklist2.getId()) {
								b = true;
								break;
							}
						}
						if (!b)
							lvBooklistData.add(booklist1);
					}
				} else {
					lvBooklistData.addAll(blist.getBooklistList());
				}
				
				break;
				
			case UIHelper.LISTVIEW_DATATYPE_MYBOOKLIST:
			
				BooklistList plist = (BooklistList) obj;
				notice = plist.getNotice();
				lvMyBooklistSumData += what;
				if (lvMyBooklistData.size() > 0) {
					for (Booklist booklist1 : plist.getBooklistList()) {
						boolean b = false;
						for (Booklist booklist2 : lvMyBooklistData) {
							if (booklist1.getId() == booklist2.getId()) {
								b = true;
								break;
							}
						}
						if (!b)
							lvMyBooklistData.add(booklist1);
					}
				} else {
					lvMyBooklistData.addAll(plist.getBooklistList());
				}
				
				break;
				
			case UIHelper.LISTVIEW_DATATYPE_MYTRENDS:
				
				BehaviorList tlist = (BehaviorList) obj;
				notice = tlist.getNotice();
				lvMyTrendsSumData += what;
				if (lvMyTrendsData.size() > 0) {
					for (Behavior behavior1 : tlist.getBehaviorList()) {
						boolean b = false;
						for (Behavior behavior2 : lvMyTrendsData) {
							if (behavior1.getId() == behavior2.getId()) {
								b = true;
								break;
							}
						}
						if (!b)
							lvMyTrendsData.add(behavior1);
					}
				} else {
					lvMyTrendsData.addAll( tlist.getBehaviorList() );
				}
				break;

			case UIHelper.LISTVIEW_DATATYPE_MYBOOKSHELF:
				
				BookshelfList alist = (BookshelfList) obj;
				notice = alist.getNotice();
				lvMyBookshelfSumData += what;
				if (lvMyBookshelfData.size() > 0) {
					for (Bookshelf bookshelf1 : alist.getBookshelfList()) {
						boolean b = false;
						for (Bookshelf bookshelf2 : lvMyBookshelfData) {
							if (bookshelf1.getId() == bookshelf2.getId()) {
								b = true;
								break;
							}
						}
						if (!b)
							lvMyBookshelfData.add(bookshelf1);
					}
				} else {
					lvMyBookshelfData.addAll(alist.getBookshelfList());
				}
				
				break;

			case UIHelper.LISTVIEW_DATATYPE_MESSAGE:
				
				BehaviorList mlist = (BehaviorList) obj;
				notice = mlist.getNotice();
				lvMyMessageSumData += what;
				if (lvMyMessageData.size() > 0) {
					for (Behavior behavior1 : mlist.getBehaviorList()) {
						boolean b = false;
						for (Behavior behavior2 : lvMyMessageData) {
							if (behavior1.getId() == behavior2.getId()) {
								b = true;
								break;
							}
						}
						if (!b)
							lvMyMessageData.add(behavior1);
					}
				} else {
					lvMyMessageData.addAll(mlist.getBehaviorList());
				}
				
				break;
			}
			break;
		}
		
		return notice;
	}

	/**
	 * 线程加载最新动态数据
	 * @param catalog
	 *            -1 热门，0 最新，大于0 某用户的动弹(uid)
	 * @param pageIndex
	 *            当前页数
	 * @param handler
	 *            处理器
	 * @param action
	 *            动作标识
	 *            
	 */
	private void loadLvTrendsData(final int catalog, final int pageIndex, final Handler handler, final int action) {
		
		mHeadProgress.setVisibility(ProgressBar.VISIBLE);
		
		new Thread() {
			public void run() {
				Message msg = new Message();
				
				boolean isRefresh = false;
				if (action == UIHelper.LISTVIEW_ACTION_REFRESH || action == UIHelper.LISTVIEW_ACTION_SCROLL)
					isRefresh = true;
				
				try {
					BehaviorList list = appContext.getBehaviorList(catalog, pageIndex, isRefresh);
					msg.what = list.getPageSize();
					msg.obj = list;
				} catch (AppException e) {
					e.printStackTrace();
					msg.what = -1;
					msg.obj = e;
				}
				msg.arg1 = action;
				msg.arg2 = UIHelper.LISTVIEW_DATATYPE_TRENDS;
				
				if (curTrendsCatalog == catalog)
					handler.sendMessage(msg);
			}
		}.start();			
	}
	
	/**
	 * 线程加载我的动态
	 * @param catalog
	 *            -1 热门，0 最新，大于0 某用户的动态(uid)
	 * @param pageIndex
	 *            当前页数
	 * @param handler
	 *            处理器
	 * @param action
	 *            动作标识
	 *            
	 */
	private void loadLvMyTrendsData(final int catalog, final int pageIndex, final Handler handler, final int action) {
		
		mHeadProgress.setVisibility(ProgressBar.VISIBLE);
		
		new Thread() {
			public void run() {
				Message msg = new Message();
				
				boolean isRefresh = false;
				if (action == UIHelper.LISTVIEW_ACTION_REFRESH || action == UIHelper.LISTVIEW_ACTION_SCROLL)
					isRefresh = true;
				
				try {
					BehaviorList list = appContext.getBehaviorList(catalog, pageIndex, isRefresh);
					msg.what = list.getPageSize();
					msg.obj = list;
				} catch (AppException e) {
					e.printStackTrace();
					msg.what = -1;
					msg.obj = e;
				}
				msg.arg1 = action;
				msg.arg2 = UIHelper.LISTVIEW_DATATYPE_MYTRENDS;
				
				handler.sendMessage(msg);
			}
		}.start();			
	}

	/**
	 * 线程加载书单列表数据
	 *  
	 * @param catalog 		分类
	 * @param pageIndex		当前页数
	 * @param handler		 处理器
	 * @param action		 动作标识
	 */
	private void loadLvBooklistData(final int catalog, final int pageIndex, final Handler handler, final int action) {
		
		mHeadProgress.setVisibility(ProgressBar.VISIBLE);
		
		new Thread() {
			public void run() {
				Message msg = new Message();
				boolean isRefresh = false;
				if (action == UIHelper.LISTVIEW_ACTION_REFRESH || action == UIHelper.LISTVIEW_ACTION_SCROLL)
					isRefresh = true;
				
				try {
					BooklistList list = appContext.getBooklistList(catalog, pageIndex, -1, isRefresh);
					msg.what = list.getPageSize();
					msg.obj = list;
				} catch (AppException e) {
					e.printStackTrace();
					msg.what = -1;
					msg.obj = e;
				}
				msg.arg1 = action;
				msg.arg2 = UIHelper.LISTVIEW_DATATYPE_BOOKLIST;
				if (curBooklistCatalog == catalog)
					handler.sendMessage(msg);
			}
		}.start();
	}

	/**
	 * 线程加载我的书单数据
	 * 
	 * @param catalog   分类
	 * @param pageIndex 当前页数
	 * @param handler   处理器
	 * @param action    动作标识
	 */
	private void loadLvMyBooklistData(final int catalog, final int pageIndex, final Handler handler, final int action) {
		
		mHeadProgress.setVisibility(ProgressBar.VISIBLE);
		
		new Thread() {
			public void run() {
				Message msg = new Message();
				
				boolean isRefresh = false;
				if (action == UIHelper.LISTVIEW_ACTION_REFRESH || action == UIHelper.LISTVIEW_ACTION_SCROLL)
					isRefresh = true;
				
				try {
					BooklistList list = appContext.getBooklistList(catalog, pageIndex, appContext.getLoginUid(), isRefresh);
					msg.what = list.getPageSize();
					msg.obj = list;
				} catch (AppException e) {
					e.printStackTrace();
					msg.what = -1;
					msg.obj = e;
				}
				msg.arg1 = action;
				msg.arg2 = UIHelper.LISTVIEW_DATATYPE_MYBOOKLIST;
				if (curMyBooklistCatalog == catalog)
					handler.sendMessage(msg);
			}
		}.start();
	}

	/**
	 * 线程加载动态数据
	 * 
	 * @param catalog
	 * @param pageIndex
	 *            当前页数
	 * @param handler
	 * @param action
	 */
	private void loadLvMyBookshelfData(final int catalog, final int pageIndex, final Handler handler, final int action) {
		mHeadProgress.setVisibility(ProgressBar.VISIBLE);
		
		new Thread() {
			public void run() {
				Message msg = new Message();
				boolean isRefresh = false;
				if (action == UIHelper.LISTVIEW_ACTION_REFRESH
						|| action == UIHelper.LISTVIEW_ACTION_SCROLL)
					isRefresh = true;
				try {
					BookshelfList list = appContext.getBookshelfList(catalog, pageIndex, isRefresh);
					msg.what = list.getPageSize();
					msg.obj = list;
				} catch (AppException e) {
					e.printStackTrace();
					msg.what = -1;
					msg.obj = e;
				}
				msg.arg1 = action;
				msg.arg2 = UIHelper.LISTVIEW_DATATYPE_MYBOOKSHELF;
				
				handler.sendMessage(msg);
				
			}
		}.start();	
	}

	/**
	 * 线程加载 最新通知
	 * 
	 * @param pageIndex
	 *            当前页数
	 * @param handler
	 * @param action
	 */
	private void loadLvMyMessageData(final int catalog, final int pageIndex, final Handler handler, final int action ) {
		mHeadProgress.setVisibility(ProgressBar.VISIBLE);
		
		new Thread() {
			public void run() {
				Message msg = new Message();
				boolean isRefresh = false;
				if (action == UIHelper.LISTVIEW_ACTION_REFRESH || action == UIHelper.LISTVIEW_ACTION_SCROLL)
					isRefresh = true;
				try {
					BehaviorList list = appContext.getMessageList(catalog, pageIndex, isRefresh);
					msg.what = list.getPageSize();
					msg.obj = list;
				} catch (AppException e) {
					e.printStackTrace();
					msg.what = -1;
					msg.obj = e;
				}
				msg.arg1 = action;
				msg.arg2 = UIHelper.LISTVIEW_DATATYPE_MESSAGE;
				
				handler.sendMessage(msg);
			}
		}.start();
		
	}

	/**
	 * 轮询通知信息
	 */
	private void foreachUserNotice() {

		final int uid = appContext.getLoginUid();
		final Handler handler = new Handler() {
			public void handleMessage(Message msg) {
				if (msg.what == 1) {
					UIHelper.sendBroadCast(Main.this, (Notice) msg.obj);
				}
				foreachUserNotice();// 回调
			}
		};
		new Thread() {
			public void run() {
				Message msg = new Message();
				try {
					sleep(60 * 1000);
					if (uid > 0) {
						Notice notice = appContext.getUserNotice(uid);
						msg.what = 1;
						msg.obj = notice;
					} else {
						msg.what = 0;
					}
				} catch (AppException e) {
					e.printStackTrace();
					msg.what = -1;
				} catch (Exception e) {
					e.printStackTrace();
					msg.what = -1;
				}
				handler.sendMessage(msg);
			}
		}.start();
	
	}
	
	/**
	 * 通知信息处理
	 * 
	 * @param type
	 * 1:@我的信息  2:未读消息  3:评论个数  4:新粉丝个数
	 */
	private void ClearNotice(final int type) {
		
		final int uid = appContext.getLoginUid();
		
		final Handler handler = new Handler() {
			public void handleMessage(Message msg) {
				if (msg.what == 1 && msg.obj != null) {
					MyResult res = (MyResult) msg.obj;
					if (res.OK() && res.getNotice() != null) {
						UIHelper.sendBroadCast(Main.this, res.getNotice());
					}
				} else {
					((AppException) msg.obj).makeToast(Main.this);
				}
			}
		};
		
		new Thread() {
			public void run() {
				Message msg = new Message();
				try {
					MyResult res = appContext.noticeClear(uid, type);
					msg.what = 1;
					msg.obj = res;
				} catch (AppException e) {
					e.printStackTrace();
					msg.what = -1;
					msg.obj = e;
				}
				handler.sendMessage(msg);
			}
		}.start();
	}

	/**
	 * 创建menu TODO 停用原生菜单
	 */
	public boolean onCreateOptionsMenu(Menu menu) {
		return false;
	// 	MenuInflater inflater = getMenuInflater();
	// 	inflater.inflate(R.menu.main_menu, menu);
	// 	return true;
	}

	/**
	 * 菜单被显示之前的事件
	 */
	public boolean onPrepareOptionsMenu(Menu menu) {
		UIHelper.showMenuLoginOrLogout(this, menu);
		return true;
	}

	/**
	 * 处理menu的事件
	 */
	public boolean onOptionsItemSelected(MenuItem item) {
		int item_id = item.getItemId();
		switch (item_id) {
		case R.id.main_menu_user:
			UIHelper.loginOrLogout(this);
			break;
		case R.id.main_menu_about:
			UIHelper.showAbout(this);
			break;
		case R.id.main_menu_setting:
			UIHelper.showSetting(this);
			break;
		case R.id.main_menu_exit:
			UIHelper.Exit(this);
			break;
		}
		return true;
	}

	/**
	 * 监听返回--是否退出程序
	 */
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		boolean flag = true;
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			// 是否退出应用
			UIHelper.Exit(this);
		} else if (keyCode == KeyEvent.KEYCODE_MENU) {
			// 展示快捷栏&判断是否登录
			UIHelper.showSettingLoginOrLogout(Main.this, mGrid.getQuickAction(0));
			mGrid.show(fbSetting, true);
		} else if (keyCode == KeyEvent.KEYCODE_SEARCH) {
			// 展示搜索页
			UIHelper.showSearch(Main.this);
		} else {
			flag = super.onKeyDown(keyCode, event);
		}
		return flag;
	}

}
